'''RobotToolBar

A toolbar for working with robot objects
'''

import wx
import wx.lib.agw.aui as aui
from wx.lib.embeddedimage import PyEmbeddedImage

ID_RUN_RSA = wx.NewId()
ID_RUN_RSA_TESTSUITE = wx.NewId()
ID_RUN_RSA_TESTCASE = wx.NewId()

class RobotToolBar(aui.AuiToolBar):

    name = "Robot Toolbar"
    _id = "rwb.toolbar.robot"
    
    def __init__(self, parent):
        aui.AuiToolBar.__init__(self, parent, wx.ID_ANY, style=aui.AUI_TB_TEXT)

        self.window = parent
        rsa_bitmap = getRSABitmap()

        # The RSA plugin probably should be doing this, maybe
        # like:
        # robot_toolbar = firebrick.app.GetToolbar("rwb.toolbar.robot")
        # robot_toolbar.AddTool...
        self.AddTool(ID_RUN_RSA, "RSA", 
                           rsa_bitmap, wx.NullBitmap, wx.ITEM_NORMAL,
                           "RSA", "run RSA", None)
        self.SetToolDropDown(ID_RUN_RSA, True)


        parent.auimgr.AddPane(self, aui.AuiPaneInfo().
                              ToolbarPane().
                              Top().
                              Floatable(False).
                              Gripper(True).
                              Name("rwb.toolbar.robot").
                              Movable(True).
                              Resizable(False).
                              Show(False)
                              )
        self.Bind(aui.EVT_AUITOOLBAR_TOOL_DROPDOWN, self.OnRSADropDown, id=ID_RUN_RSA)
       
        wx.EVT_MENU(self, ID_RUN_RSA, self.OnRSA)

        self.Realize()

        self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateUI)

    def OnUpdateUI(self, event):
        # *sigh* This seems *so* compute intensive since it happens
        # every time the mouse moves a single pixel. I sure wish
        # wxPython were smarter. Maybe I can do this on change of
        # focus instead?
        event_id = event.GetId()
        editor = self.window.GetCurrentEditor()
        enable = False
        if event_id == ID_RUN_RSA:
            enable = (editor is not None and 
                      hasattr(editor, "GetRobotSuite") and
                      editor.GetRobotSuite() is not None)
            event.Enable(enable)
        else:
            event.Skip()


    def OnRSADropDown(self, event):
        if event.IsDropDownClicked():
            tb = event.GetEventObject()
            tb.SetToolSticky(event.GetId(), True)

            menuPopup = wx.Menu()
            bmp = wx.ArtProvider.GetBitmap(wx.ART_QUESTION, wx.ART_OTHER, wx.Size(16, 16))

            m1 = wx.MenuItem(menuPopup, ID_RUN_RSA_TESTSUITE, "Run RSA on Test &Suite")
            m2 = wx.MenuItem(menuPopup, ID_RUN_RSA_TESTCASE, "Run RSA on Test &Case")
            menuPopup.AppendItem(m1)
            menuPopup.AppendItem(m2)

            # line up our menu with the button
            rect = tb.GetToolRect(event.GetId())
            pt = tb.ClientToScreen(rect.GetBottomLeft())
            pt = self.ScreenToClient(pt)

            self.PopupMenu(menuPopup, pt)
            tb.SetToolSticky(event.GetId(), False)
                
    def OnRSA(self, event):
        print "OnRSA..."


RSA_image = PyEmbeddedImage(
    "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0"
    "RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAGSSURBVCjPVVFNSwJhEF78Ad79"
    "Cf6PvXQRsotUlzKICosuRYmR2RJR0KE6lBFFZVEbpFBSqKu2rum6llFS9HHI4iUhT153n6Zt"
    "IWMOM+/MM88z7wwH7s9Ub16SJcnbmrNcxVm2q7Z8/QPvEOtntpj92NkCqITLepEpjix7xQti"
    "LOoQ2b6+E7YAN/5nfOEJ2WbKqOIOJ4bYVMEQx4LfBBQDsvFMhUcCVU1/CxVXmDBGA5ZETrhD"
    "CQVcYAPbyEJBhvrnBVPiSpNr6cYDNCQwo4zzU/ySckkgDYuNuVpI42T9k4gLKGMPs/xPzzov"
    "QiY2hQYe0jlJfyNNhTqiWDYBq/wBMcSRpnyPzu1oS7WtxjVBSthU1vgVksiQ3Dn6Gp5ah2YO"
    "KQo5GiuHPA6xT1EKpxQNCNYejgIR457KKio0S56YckjSa9jo//3mrj+BV0QQagqGTOo+Y7gZ"
    "If1puP3WHoLhEb2PjTlCTCWGXtbp8DCX3hZuOdaIc9A+aQvWk4ihq95p67a7nP+u+Ws+r0dq"
    "l9z/zv0NCYhdCPKZ7oYAAAAASUVORK5CYII=")
getRSABitmap = RSA_image.GetBitmap
